package control;
import java.lang.String;


/**
 * EpoFilter
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 */
public class EpoFilter{

	/**
	 * construtor EpoFilter
	 */
	public EpoFilter(){			
	}


	/**
	 * Filter EpoFilter
	 * @param searchResult searchResult
	 * @return s
	 */
	public String filter(String searchResult){
		String	 s = searchResult;
		int		 beginIndex = s.indexOf("Print");
		s = s.substring( beginIndex );		
		//apply espacenet filter to search result
		s = s.replace("Print", "Title:");
		s = s.replaceAll("in my patents list","");
		s = s.replaceAll("document.write.makeEcla.", "");
		s = s.replace("Data supplied from the", "");
		s = s.replace("esp@cenet", "");
		s = s.replace("database - Worldwide", "");
		s = s.replace("View INPADOC patent family", "");
		s = s.replace("View list of citing documents", "");
		s = s.replace("Report a data error here", "");
		return s;
	}


	/**
	 * FiltroBusca
	 * seleciona lista de resultados do espacenet
	 * seleciona partes relevantes da busca no espacenet
	 * @param s search result
	 * @return resultado
	 */
	public String filtroBusca( String s ){				
		String	 selectText = "";
		int 	 beginIndex;
		while(s.contains("resultList")){
			beginIndex = s.indexOf("resultList");
			s = s.substring(beginIndex+"resultList".length());
			int 	endIndex = s.indexOf("</table>");
			System.out.println("begin "+beginIndex+" end "+ endIndex);
			selectText = selectText+s.substring(1, endIndex-1);
		}
		selectText = selectText.replaceAll("in my patents list","");
		selectText = selectText.replaceAll("document.write.makeEcla.", "");
		selectText = selectText.replaceAll("\\)\\)","");
		selectText = selectText.replaceAll("\\'\\'","");
		selectText = selectText.replaceAll("width=","<width=");

		String resultado = cleanHtml(selectText);		
		return resultado;
	}



	/**
	 * CleanHtml
	 * limpeza do cabecalho de busca para cada pagina do esp@cenet	
	 * @param str str
	 * @return str
	 */
	public String cleanHtml( String str ){
		str = str.replaceAll("<.*?>", "");
		str = str.replaceAll("&nbsp;"," ");
		str = str.replaceAll("&gt;",">");
		str = str.replaceAll("&lt;","<");
		str = str.replaceAll(" *?\n *?\n", "\n");// apaga linhas vazias
		str = str.replaceAll("\n\n+","\n");
		return str;
	}

}